package cn.iocoder.yudao.module.cmt.service.team;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import cn.iocoder.yudao.module.cmt.controller.admin.team.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.team.TeamInfoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.cmt.dal.mysql.team.TeamInfoMapper;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.cmt.enums.ErrorCodeConstants.*;

@Service
@Validated
public class TeamInfoServiceImpl implements TeamInfoService {

    @Resource
    private TeamInfoMapper teamInfoMapper;

    @Override
    public Long createTeamInfo(TeamInfoSaveReqVO createReqVO) {
        TeamInfoDO teamInfo = BeanUtils.toBean(createReqVO, TeamInfoDO.class);
        teamInfoMapper.insert(teamInfo);
        return teamInfo.getId();
    }

    @Override
    public void updateTeamInfo(TeamInfoSaveReqVO updateReqVO) {
        validateTeamInfoExists(updateReqVO.getId());
        TeamInfoDO updateObj = BeanUtils.toBean(updateReqVO, TeamInfoDO.class);
        teamInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteTeamInfo(Long id) {
        validateTeamInfoExists(id);
        teamInfoMapper.deleteById(id);
    }

    private void validateTeamInfoExists(Long id) {
        if (teamInfoMapper.selectById(id) == null) {
            throw exception(TEAM_INFO_NOT_EXISTS);
        }
    }

    @Override
    public TeamInfoDO getTeamInfo(Long id) {
        return teamInfoMapper.selectById(id);
    }

    @Override
    public PageResult<TeamInfoDO> getTeamInfoPage(TeamInfoPageReqVO pageReqVO) {
        return teamInfoMapper.selectPage(pageReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteTeamInfoListByIds(List<Long> ids) {
        ids.forEach(this::validateTeamInfoExists);
        teamInfoMapper.deleteBatchIds(ids);
    }

}
